package maps

import "gitee.com/OneBedOneDream/general/slices"

// 检查两个map是否有相同的key
func Like[KEY comparable, VALUE1, VALUE2 any](m1 map[KEY]VALUE1, m2 map[KEY]VALUE2) bool {
	if len(m1) != len(m2) {
		return false
	}
	for key := range m1 {
		_, ok := m2[key]
		if !ok {
			return false
		}
	}
	return true
}

// 检查key是否在m中
func Exist[KEY comparable, VALUE any](m map[KEY]VALUE, key KEY) bool {
	_, ok := m[key]
	return ok
}

// 返回一个保存m所有的key的slice
func Keys[KEY comparable, VALUE any](m map[KEY]VALUE) (keys []KEY) {
	keys = make([]KEY, 0, len(m))
	for key := range m {
		keys = slices.Append(keys, key)
	}
	return
}

// 返回一个保存m所有的value的slice
func Values[KEY comparable, VALUE any](m map[KEY]VALUE) (values []VALUE) {
	values = make([]VALUE, 0, len(m))
	for _, value := range m {
		values = slices.Append(values, value)
	}
	return
}

// 返回一个和m相同但是数据不共享的容器
func Copy[KEY comparable, VALUE any](m map[KEY]VALUE) (nm map[KEY]VALUE) {
	nm = make(map[KEY]VALUE, len(m))
	for key, value := range m {
		nm[key] = value
	}
	return
}
